package com.demo.framework.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;

import com.demo.framework.utils.IOUtils;

public class HttpRequest extends BaseRequest {

    private int responseCode = -1;

    // 超时时间
    protected int connectTimeout = 60*1000;

    protected int readTimeout = 60*1000;
    
    protected Map<String, String> properties = new Hashtable<String, String>();
    /**
     * 设置HTTP Request 属性
     * @param name 属性名称
     * @param vlaue 属性值
     */
    public void setRequestProperty(String name, String value) {
        properties.put(name, value);

    }
    
    public HttpRequest(){
        
    }

    /**
     * 
     * @Title: setConnectTimeout
     * @Description: TODO(设置网络链接超时 时间)
     * @return void 返回类型
     * @throws
     */
    protected void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    protected void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    @Override
    public void post(String url, InputStream input, int length, OutputStream output) throws HttpException {
        // TODO Auto-generated method stub
        doHttp(url, "POST", input, length, output);
    }

    @Override
    public void get(String url, OutputStream output) throws HttpException {
        // TODO Auto-generated method stub
        doHttp(url, "GET", null, 0, output);
    }

    private void doHttp(String url, String method, InputStream input, int length, OutputStream output)
            throws HttpException {
        HttpURLConnection conn = null;
        try {
            conn = openConnection(url, method);
            conn.setDoInput(input != null);
            conn.setDoOutput(output != null);
            if (length >= 0)
                conn.setFixedLengthStreamingMode(length);
            else
                conn.setChunkedStreamingMode(0);
            if (input != null) {
                IOUtils.copy(input, conn.getOutputStream());
            }
            responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream ri = null;
                if (conn.getDoInput()) {
                    ri = conn.getInputStream();
                }
                if (ri != null) {
                    ri.close();
                }
            }
            else {
            }
        }
        catch(Exception e) {
            throw HttpException.urlConnectionException(e);
        } finally {
            closeConnection(conn);
        }

    }

    private void closeConnection(HttpURLConnection conn) {
        if (conn != null)
            conn.disconnect();
    }

    /**
     * 打开网络接口，设置超时时间
     * 
     * @Title: openConnection
     * @param @param urlAdress url
     * @param @param method post\get
     * @param @return
     * @param @throws HttpException
     * @return HttpURLConnection 返回类型
     * @throws
     */
    private HttpURLConnection openConnection(String urlAdress, String method) throws Exception {
        assert urlAdress != null;
        URL url = new URL(urlAdress);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(connectTimeout);
        conn.setReadTimeout(readTimeout);
        conn.setRequestMethod(method);
        
        for (Entry<String, String> prop : properties.entrySet()) {
            conn.setRequestProperty(prop.getKey(), prop.getValue());
        }
        return conn;
    }

}
